package no.ntnu.fp.gui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;

import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.SwingConstants;

import run.Main;

import no.ntnu.fp.model.Employee;
import no.ntnu.fp.net.DBConnection;

import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;

public class Login extends JFrame {
	private JTextField textField;
	private JPasswordField passwordField;
	
	public Login() {
		getContentPane().setBackground(Color.WHITE);
		setResizable(false);
		initialiseUI();
		setLocationRelativeTo(null);
	}
	
	private void login() {
		String username = textField.getText();
		String password = new String(passwordField.getPassword());
		
		boolean isValid = checkValidUser(username, password);
		
		if (!isValid) {
			showUserInvalidPopup();
			return;
		}
		
		// JOptionPane.showMessageDialog(this, "User logged in: " + Main.user.getName()); TODO: remove debug
		this.setVisible(false);
		showMainWindow();
	}
	
	private void showMainWindow() {  //just for now when we login it goes to the ManageAppointment view (doesn't work..)
		this.setVisible(false);
		Main.mainFrame = new MainFrame();
		Main.mainFrame.setVisible(true);
		
	}
	
	private void showUserInvalidPopup() {
		JOptionPane.showMessageDialog(this, "Incorrect username or password", "Wrong user credentials", JOptionPane.ERROR_MESSAGE);
	}
	
	private boolean checkValidUser(String username, String password) {
		
		try {
			ResultSet userRes = Main.dbConn.selectFromDatabase("SELECT * FROM employee " +"WHERE " 
			+"username = '" + username + "' AND " 
			+"password = '" + password +"'");
			
			if(userRes.next()){
				Main.user = new Employee(
				userRes.getInt("employeeId"),		
				userRes.getString("username"),
				userRes.getString("name"),
				userRes.getString("password"),
				userRes.getString("email")
				);
				return true;
			}
			
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(this, "Connection error","Could not connect to database" , JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		
		
		return false;
	}
	
	private void registerNewUser() {
		this.setVisible(false); //makes the login screen not visible, and the CreateNewUser screen visible 
		
		CreateNewUser frame = new CreateNewUser();
		frame.setVisible(true);
	}
	
	private void initialiseUI() { //the user interface layout
		//a crazy wall of code that the design thing created
		setMinimumSize(new Dimension(340, 180));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Calendar System - Login");
		getContentPane().setLayout(null);
		
		JLabel lblElectronicCalendarSystem = new JLabel("Electronic Calendar System");
		lblElectronicCalendarSystem.setBounds(45, 6, 263, 31);
		lblElectronicCalendarSystem.setHorizontalAlignment(SwingConstants.RIGHT);
		lblElectronicCalendarSystem.setFont(new Font("Tahoma", Font.PLAIN, 22));
		getContentPane().add(lblElectronicCalendarSystem);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(19, 50, 83, 14);
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		getContentPane().add(lblUsername);
		
		textField = new JTextField();
		textField.setBounds(111, 42, 197, 28);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(19, 83, 81, 14);
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		getContentPane().add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
			     if (key == KeyEvent.VK_ENTER) {
			    	 login();
			        }
			}
		});
		passwordField.setBounds(111, 75, 197, 28);
		getContentPane().add(passwordField);
		
		JButton btnLogIn = new JButton("Log in");
		btnLogIn.setBounds(225, 115, 83, 29);
		btnLogIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		getContentPane().add(btnLogIn);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(111, 115, 95, 29);
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				registerNewUser();
			}
		});
		getContentPane().add(btnRegister);
	}

}
